import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '@/view/Login.vue'
// import Home from '@/view/Home.vue'
// import WelCome from '@/components/WelCome.vue'
const Login = () => import(/* webpackChunkName: "login_home_weblCome" */ '@/view/Login.vue')
const Home = () => import(/* webpackChunkName: "login_home_weblCome" */ '@/view/Home.vue')
const WelCome = () => import(/* webpackChunkName: "login_home_weblCome" */ '@/components/WelCome.vue')


// import Users from '@/view/user/Users.vue'
// import Rights from '@/view/power/Rights.vue'
// import Roles from '@/view/power/Roles.vue'
const Users = () => import(/* webpackChunkName: "user_rights_roles" */ '@/view/user/Users.vue')
const Rights = () => import(/* webpackChunkName:"user_rights_roles" */ '@/view/power/Rights.vue')
const Roles = () => import(/* webpackChunkName: "user_rights_roles" */ '@/view/power/Roles.vue')


// import Cate from '@/view/goods/Cate.vue'
// import Params from '@/view/goods/Params.vue'
const Cate = () => import(/* webpackChunkName:  "cate_params" */ '@/view/goods/Cate.vue')
const Params = () => import(/* webpackChunkName:"cate_params" */ '@/view/goods/Params.vue')



// import List from '@/view/goods/List.vue'
// import GoodsAdd from '@/view/goods/Add.vue'
const List = () => import(/* webpackChunkName:    "list_goods" */ '@/view/goods/List.vue')
const GoodsAdd = () => import(/* webpackChunkName:"list_goods" */ '@/view/goods/Add.vue')
const GoodEdit = () => import(/* webpackChunkName:"list_goods" */ '@/view/goods/Edit.vue')


// import Order from '@/view/order/Order.vue'
// import Reports from '@/view/reports/Reports.vue'
const Order = () => import(/* webpackChunkName:  "order_reports" */ '@/view/order/Order.vue')
const Reports = () => import(/* webpackChunkName:"order_reports" */ '@/view/reports/Reports.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welCome',
    children: [
      { path: '/welCome', component: WelCome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: List },
      { path: '/goods/add', component: GoodsAdd },
      { path: '/goods/edit', component: GoodEdit },
      { path: '/orders', component: Order },
      { path: '/reports', component: Reports },
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
// to -- > 路由去哪里 ，form --> 路由从哪里来  next --> 放行 （1 直接放行）（2 强制拦截）
router.beforeEach((to, form, next) => {
  // 判断是不是访问login 如果是就放行
  if (to.path === '/login') return next()
  // 判断有没有token认证
  const tokenStr = sessionStorage.getItem('token')
  // 没有就强制跳转登录 有就放行
  if (!tokenStr) return next('/login')
  next()
})

export default router
